<template>
	<view>
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{data.title}}</block>
		</cu-custom>
		<view>
			<view class="padding-sm margin-top text-gray text-lg">多媒体</view>
			<view class="cu-form-group">
				<view class="title">非WIFI自动播放</view>
				<switch class="yellow" @change="switchPlayer" data-key="autoplay" :class="data.setting.player.autoplay==1?'checked':''" :checked="data.setting.player.autoplay==1?true:false"></switch>
			</view>
			<view class="cu-list menu solid-top" @click="showmodal='playmode'">
				<view class="cu-item arrow">
					<view class="content">
						<text>默认播放画质</text>
					</view>
					<view class="action">
						<view class="cu-tag round bg-orange light">{{playmodes[data.setting.player.playmode]}}</view>
					</view>
				</view>
			</view>
			<view class="padding-sm margin-top text-gray text-lg">消息通知</view>
			<view class="cu-form-group">
				<view class="title">接收站内提醒<text class="text-xs text-gray margin-left-sm">如评论、回复等提醒</text></view>
				<switch class="yellow" @change="switchPush" data-key="notice" :class="data.setting.push.notice==1?'checked':''" :checked="data.setting.push.notice==1?true:false"></switch>
			</view>
			<view class="cu-form-group">
				<view class="title">接收系统通知<text class="text-xs text-gray margin-left-sm">系统通知公告</text></view>
				<switch class="yellow" @change="switchPush" data-key="system" :class="data.setting.push.system==1?'checked':''" :checked="data.setting.push.system==1?true:false"></switch>
			</view>
			<view class="cu-form-group">
				<view class="title">接收推荐消息<text class="text-xs text-gray margin-left-sm">优惠活动、中奖信息</text></view>
				<switch class="yellow" @change="switchPush" data-key="recommend" :class="data.setting.push.recommend==1?'checked':''" :checked="data.setting.push.recommend==1?true:false"></switch>
			</view>
			<view class="margin-top-xl bg-white text-lg">
				<view class="text-center padding" @click="doLogout">
					<text class="text-orange">退出当前账号</text>
				</view>
				<view class="text-center padding solid-top" v-if="platform=='app'" @click="doQuitapp">
					<text class="text-gray">关闭应用</text>
				</view>
			</view>
		</view>
		<view class="cu-modal bottom-modal" :class="showmodal=='playmode'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white">
					<view class="action" @tap="showmodal=''">取消</view>
					<view class="action text-blue" @click="updateSet('player',data.setting.player)">确定</view>
				</view>
				<view class="grid col-3 padding-sm">
					<view class="padding-xs">
						<button class="cu-btn orange lg block" :class="data.setting.player.playmode=='no'?'bg-orange':'line-orange'" @tap="data.setting.player.playmode='no'">原画</button>
					</view>
					<view class="padding-xs">
						<button class="cu-btn orange lg block" :class="data.setting.player.playmode=='hd'?'bg-orange':'line-orange'" @tap="data.setting.player.playmode='hd'">高清</button>
					</view>
					<view class="padding-xs">
						<button class="cu-btn orange lg block" :class="data.setting.player.playmode=='sd'?'bg-orange':'line-orange'" @tap="data.setting.player.playmode='sd'">标清</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import core from "@/static/core.js"
	
	export default {
		data() {
			return {
				platform:'h5',
				showmodal:'',
				playmodes:{
					'no':'原画',
					'hd':'高清',
					'sd':'标清'
				},
				data:{
					title:"偏好设置",
					setting:{
						withdraw:{
							"method":""
						},
						player:{
							"autoplay":1,
							"playmode":'no'
						},
						push:{
							"notice":1,
							"system":1,
							"recommend":1
						}
					}
				}
			}
		},
		onLoad() {
			this.platform = core.platform;
			this.initData('member/setting');
		},
		onShow() {
			
		},
		methods:{
			initData(route, data={}, callback=false){
				var that = this;
				core.get(route,function(res){
					// if(res===null || typeof(res.message)!='undefined' && typeof(res.type)!='undefined'){
					// 	return core.report(res, true);
					// }
					that.data = res;
					uni.setNavigationBarTitle({
						title:res.title
					});
					if(callback){
						callback(res);
					}
				},data);
			},
			naviTo(page,data={}){
				return core.navito(page,data);
			},
			updateSet(key='',data){
				if(typeof(data)=='undefined'){
					 let data = '';
				}
				var that = this;
				return core.post('member/setting',function(res){
					if(res.type!='success') core.report(res);
					that.showmodal = '';
				},{data:data,setkey:key});
			},
			switchPlayer(e){
				let setkey = e.target.dataset.key;
				this.data.setting.player[setkey] = e.detail.value?1:0;
				var that = this;
				return this.updateSet('player',this.data.setting.player);
			},
			switchPush(e){
				let setkey = e.target.dataset.key;
				this.data.setting.push[setkey] = e.detail.value?1:0;
				var that = this;
				return this.updateSet('push',this.data.setting.push);
			},
			doLogout(){
				return core.confirm('确定要退出当前账号重新登录吗？','切换账号',function(){
					if(core.logout()){
						core.navito('auth/index',{act:'login'},1);
					}
				});
			},
			doQuitapp(){
				core.confirm('确定要退出？',null,function(){
					plus.runtime.quit(); 
				});
			}
		}
	}
</script>

<style>
	page{background: #F7F7F7;}
</style>
